package com.rd.student.jiaji.class1.jinhongyu._0712;


import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;

@SuppressWarnings("all")
public class Test01 {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        Student g1 = new Student("金洪宇", "加计", "1", "a", 20, 1);
        Student g2 = new Student("金洪宇1", "加计", "1", "a", 40, 2);
        Student g3 = new Student("金洪宇2", "加", "2", "a", 60, 3);
        Student g4 = new Student("金洪宇3", "加", "2", "a", 80, 4);

        list.add(g1);
        list.add(g2);
        list.add(g3);
        list.add(g4);
        while (true) {
            menu();
            Scanner scanner = new Scanner(System.in);
            int i = Integer.parseInt(scanner.nextLine());
            switch (i) {
                case 0:
                    viewAll(list);
                    break;
                case 1:
                    addUtils(list);
                    break;
                case 2:
                    removeUtils(list);
                    break;
                case 3:
                    getClassStudent(list);
                    break;
                case 4:
                    getMajorStudent(list);
                    break;
                case 5:
                    getAgeStudent(list);
                    break;
                case 6:
                    NameUtils(list);
                    break;
                case 7:
                    AgeUtils(list);
                    break;
                case 8:
                    LinkedList list1 = ageRange(list, 20, 50);
                    System.out.println(list1);
                    break;
                case 9:
                    return;
            }
        }
    }


    public static void menu() {
        System.out.println("0、查看所有小组成员");
        System.out.println("1、新增小组成员");
        System.out.println("2、移除小组成员");
        System.out.println("3、根据班级查询成员列表");
        System.out.println("4、根据专业查询成员列表 ");
        System.out.println("5、根据年龄查询成员列表 ");
        System.out.println("6、返回所有成员名字");
        System.out.println("7、计算成员的平均年龄");
        System.out.println("8、根据年龄范围返回成员列表");
        System.out.println("请输入对应数字");
    }

    //0
    public static void viewAll(LinkedList linkedList) {
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
    }

    //1
    public static void addUtils(LinkedList linkedList) {
        Student g1 = new Student();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入这个成员的姓名");
        g1.setName(scanner.nextLine());
        System.out.println("请输入这个成员的年龄（数字）");
        g1.setAge(Integer.parseInt(scanner.nextLine()));
        System.out.println("请输入这个成员的班级（1 2 3 4）");
        g1.set_Class(scanner.nextLine());
        System.out.println("请输入这个成员的专业");
        g1.setMajor(scanner.nextLine());
        System.out.println("请输入这个成员的描述");
        g1.setDescribe(scanner.nextLine());
        int studentID = linkedList.size() + 1;
        g1.setStudentID(studentID);

        boolean isDuplicate = false;
        for (int i = 0; i < linkedList.size(); i++) {
            Student g = (Student) linkedList.get(i);
            if (g1.getName().equals(g.getName())) {
                isDuplicate = true;
                break;
            }
        }
        if (isDuplicate) {
            System.out.println("插入失败,姓名重复");
        } else {
            linkedList.add(g1);
            System.out.println("插入成功，打印全部数据");
            viewAll(linkedList);
        }
    }

    //2
    public static void removeUtils(LinkedList linkedList) {
        boolean isRemoved = false;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要移除的人姓名:");
        String name = scanner.nextLine();
        for (int i = 0; i < linkedList.size(); i++) {
            Student s = (Student) linkedList.get(i);
            if (s.getName().equals(name)) {
                if (isOnlyMemberInClass(linkedList)) {
                    System.out.println("处理失败，此成员所在班级只有一人，暂定不可移除");
                } else {
                    linkedList.remove(i);
                    isRemoved = true;
                    System.out.println("移除成功");
                }
                break;
            }
        }
        if (!isRemoved) {
            System.out.println("处理失败，此成员不存在");
        }
    }

    public static boolean isOnlyMemberInClass(LinkedList linkedList) {
        int count = linkedList.size();
        return count == 1;
    }

    //3
    public static int getClassStudent(LinkedList linkedList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("根据班级查询成员列表:");
        System.out.println("输入班级：(1 2 3 4)");
        String className = (scanner.nextLine());
        int count = 0;
        for (int i = 0; i < linkedList.size(); i++) {
            Student student = (Student) linkedList.get(i);
            if (student.get_Class().equals(className)) {
                System.out.println(className + " " + student);
            }
        }
        return count;
    }

    //4
    public static int getMajorStudent(LinkedList linkedList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("根据专业查询成员列表:");
        System.out.println("输入专业：");
        String major = scanner.nextLine();
        int count = 0;
        for (int i = 0; i < linkedList.size(); i++) {
            Student student = (Student) linkedList.get(i);
            if (student.getMajor().equals(major)) {
                System.out.println(major + " " + student);
            }
        }
        return count;
    }

    //5
    public static int getAgeStudent(LinkedList linkedList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("根据年龄查询成员列表:");
        System.out.println("输入年龄：");
        int age = Integer.parseInt(scanner.nextLine());
        int count = 0;
        for (int i = 0; i < linkedList.size(); i++) {
            Student student = (Student) linkedList.get(i);
            if (student.getAge() == age) {
                System.out.println(age + " " + student);
            }
        }
        return count;

    }

    //6
    public static void NameUtils(LinkedList linkedList) {
        for (int i = 0; i < linkedList.size(); i++) {
            Student g1 = (Student) linkedList.get(i);
            System.out.println(g1.getName());
        }
    }

    //7
    public static void AgeUtils(LinkedList linkedList) {
        int average = 0;
        for (int i = 0; i < linkedList.size(); i++) {
            Student g1 = (Student) linkedList.get(i);
            average += g1.getAge();
        }
        System.out.println("平均年龄：" + average / linkedList.size());//平均年龄

        Collections.sort(linkedList, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {
                Student s1 = (Student) o1;
                Student s2 = (Student) o2;
                if (s1.getAge() < s2.getAge()) return -1;
                if (s1.getAge() > s2.getAge()) return 1;
                return 0;
            }
        });//升序
        System.out.println("按年龄升序排列:");
        for (int i = 0; i < linkedList.size(); i++) {
            Student g2 = (Student) linkedList.get(i);
            System.out.println("年龄:" + g2.getAge() + " 姓名:" + g2.getName());
        }

        Collections.sort(linkedList, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {
                Student s1 = (Student) o1;
                Student s2 = (Student) o2;
                if (s1.getAge() < s2.getAge()) return 1;
                if (s1.getAge() > s2.getAge()) return -1;
                return 0;
            }
        });//降序
        System.out.println("按年龄降序排列:");
        for (int i = 0; i < linkedList.size(); i++) {
            Student g3 = (Student) linkedList.get(i);
            System.out.println("年龄:" + g3.getAge() + " 姓名:" + g3.getName());
        }
    }

    //8
    public static LinkedList ageRange(LinkedList linkedList, int minAge, int maxAge) {
        LinkedList result = new LinkedList();
        for (int i = 0; i < linkedList.size(); i++) {
            Student s = (Student) linkedList.get(i);
            if (s.getAge() >= minAge && s.getAge() <= maxAge) {
                result.add(s);
            }
        }
        return result;
    }


}